/*
 * Class PickupItem
 *
 * Version 1.0
 *
 * Thursday, August 21, 2008
 *
 * Created by Palidino76
 */

package net.com.hydroPK.net.packethandler;

import net.com.hydroPK.Engine;
import net.com.hydroPK.Server;
import net.com.hydroPK.model.Player;
import net.com.hydroPK.util.Misc;
import net.com.hydroPK.world.items.GroundItem;

public class PickupItem implements Packet {
    /**
     * Handles picking up items on the ground.
     *
     * @param p          The Player which the frame should be handled for.
     * @param packetId   The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        if (!p.itemPickup) {
            p.clickY = p.stream.readUnsignedWordA();
            p.clickX = p.stream.readUnsignedWord();
            p.clickId = p.stream.readUnsignedWordBigEndianA();
        }
        int distance = Misc.getDistance(p.clickX, p.clickY, p.absX, p.absY);
        if (distance > 0 && (p.walkDir > 0 || p.runDir > 0) || distance != 0 && p.walkDir <= 0 && p.runDir <= 0) {
            p.itemPickup = true;
            return;
        }
        p.itemPickup = false;
        int idx = Engine.items.itemExists(p.clickId, p.clickX, p.clickY, p.heightLevel);
        if (idx == -1) {
            return;
        }
        GroundItem g = Engine.items.groundItems[idx];
        if (g != null) {
            g.pickupCount++;
            if (pickupGroundItem(p, g, g.itemId, g.itemAmt)) {
                p.getActionSender().addSoundEffect(p, 2582, 1, 0, 0);
            }
        }
    }
    public boolean pickupGroundItem(Player p, GroundItem g, int itemId, int itemAmt) {
        if (g == null) {
            return false;
        }
        if (g.pickupCount > 1) {
            return false;
        }
        g.pickupCount = 0;
        if (itemId <= -1 || itemAmt <= 0 || p == null) {
            return false;
        }
        if (Engine.playerItems.freeSlotCount(p) <= 0) {
            p.getActionSender().sendMessage(p, "Not enough space in your inventory.");
            return false;
        }
        int itemsAdded = 0;
        if (!Engine.items.stackable(itemId) && !Engine.items.noted(itemId)) {
            for (int i = 0; i < p.items.length; i++) {
                int index = Engine.playerItems.findInvSlot(p);
                if (index >= 0 && itemAmt != 0) {
                    itemAmt--;
                    itemsAdded++;
                    p.items[index] = itemId;
                    p.itemsN[index] = 1;
                } else {
                    break;
                }
            }
            p.getActionSender().setItems(p, 149, 0, 93, p.items, p.itemsN);
            if (itemAmt == 0) {
                Engine.items.itemPickedup(g.itemId, g.itemX, g.itemY, g.itemHeight);
                return true;
            } else {
                Engine.items.itemPickedup(g.itemId, itemsAdded, g.itemX, g.itemY, g.itemHeight);
                return true;
            }
        } else if (Engine.items.stackable(itemId) || Engine.items.noted(itemId)) {
            if (Engine.playerItems.invItemCount(p, itemId) > 0) {
                if ((itemAmt + Engine.playerItems.invItemCount(p, itemId)) <= Engine.items.maxItemAmount) {
                    p.itemsN[Engine.playerItems.getItemSlot(p, itemId)] += itemAmt;
                    p.getActionSender().setItems(p, 149, 0, 93, p.items, p.itemsN);
                    Engine.items.itemPickedup(g.itemId, itemAmt, g.itemX, g.itemY, g.itemHeight);
                    return true;
                } else {
                    int amtToAdd = Engine.items.maxItemAmount - Engine.playerItems.invItemCount(p, itemId);
                    Engine.items.itemPickedup(g.itemId, amtToAdd, g.itemX, g.itemY, g.itemHeight);
                    p.itemsN[Engine.playerItems.getItemSlot(p, itemId)] = Engine.items.maxItemAmount;
                    p.getActionSender().setItems(p, 149, 0, 93, p.items, p.itemsN);
                    return true;
                }
            } else if (Engine.playerItems.invItemCount(p, itemId) <= 0) {
                int index = Engine.playerItems.findInvSlot(p);
                if (index >= 0) {
                    int amtToAdd = (itemAmt <= Engine.items.maxItemAmount ? itemAmt : Engine.items.maxItemAmount);
                    Engine.items.itemPickedup(g.itemId, amtToAdd, g.itemX, g.itemY, g.itemHeight);
                    p.items[index] = itemId;
                    p.itemsN[index] = amtToAdd;
                    p.getActionSender().setItems(p, 149, 0, 93, p.items, p.itemsN);
                    return true;
                } else {
                    p.getActionSender().sendMessage(p, "Not enough space in your inventory.");
                    return false;
                }
            }
        }
        p.getActionSender().sendMessage(p, "Couldn't pickup item.");
        return false;
    }
}
